package com.sugon.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.DevMainBaseDao;
import com.sugon.dao.DevPlanMainDao;
import com.sugon.entity.DevMainBaseEntity;
import com.sugon.entity.DevPlanGcEntity;
import com.sugon.entity.DevPlanGcRecordEntity;
import com.sugon.entity.DevPlanMainEntity;
import com.sugon.service.DevPlanGcRecordService;
import com.sugon.service.DevPlanGcService;
import com.sugon.service.DevPlanMainService;
import com.sugon.utils.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 设备维护计划Service实现类
 *
 * @author sugon
 * @date 2020-05-19 09:36:43
 */
@Service("devPlanMainService")
public class DevPlanMainServiceImpl implements DevPlanMainService {
    @Autowired
    private DevPlanMainDao devPlanMainDao;
    @Autowired
    private DevPlanGcService devPlanGcService;
    @Autowired
    private DevPlanGcRecordService devPlanGcRecordService;
    @Autowired
    private DevMainBaseDao devMainBaseDao;

    /**
     *
     * @param id 主键
     * @return
     */
    @Override
    public DevPlanMainEntity queryObject(String id) {
        return devPlanMainDao.queryObject(id);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<DevPlanMainEntity> queryList(Map<String, Object> map) {
        String deptCode = ShiroUtils.getDeptSubCode();
        if (deptCode.length() > 3) {
            map.put("deptCode", deptCode);
        }

        return devPlanMainDao.queryList(map);
    }

    /**
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {

        String deptCode = ShiroUtils.getDeptSubCode();
        if (deptCode.length() > 3) {
            map.put("deptCode", deptCode);
        }
        return devPlanMainDao.queryTotal(map);
    }

    /**
     *
     * @param devPlanMain 实体
     * @return
     */
    @Override
    public int save(DevPlanMainEntity devPlanMain) {
        devPlanMain.setId(RandomUtil.randomString(32));
        String dept = ShiroUtils.getDeptId();
        if (StrUtil.isNotEmpty(dept) && dept.length() > 3) {
            dept = dept.substring(0, 4);
        }
        devPlanMain.setDept(dept);
        devPlanMain.setDelFlag(1);
        devPlanMain.setTypes(2);
        devPlanMain.setCreateDate(new Date());
        devPlanMain.setApprovalStatus(0);
        devPlanMain.setIsEnd(1);
        devPlanMain.setCreateUser(ShiroUtils.getUserId());
        return devPlanMainDao.save(devPlanMain);
    }

    /**
     *
     * @param devPlanMain 实体
     * @return
     */
    @Override
    public int update(DevPlanMainEntity devPlanMain) {
        return devPlanMainDao.update(devPlanMain);
    }

    /**
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return devPlanMainDao.delete(id);
    }

    /**
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return devPlanMainDao.deleteBatch(ids);
    }

    /**
     *
     * @param devPlanMainEntity
     * @return
     */
    @Override
    public int apply(DevPlanMainEntity devPlanMainEntity) {
        devPlanMainEntity.setReview(ShiroUtils.getUserId());
        devPlanMainEntity.setAppTime(new Date());
        Integer status = devPlanMainEntity.getApprovalStatus();
        if (status == 1) {
            //审核通过   添加维护工单数据
            DevPlanMainEntity entity = queryObject(devPlanMainEntity.getId());
            //根据维护计划id查询维护规程id
            DevPlanGcEntity gc = devPlanGcService.queryObjectByPlanid(devPlanMainEntity.getId());

            Date end = entity.getEndDate();
            //判断当前时间是否大于结束时间
            if (new Date().before(end)) {
                //通过规程计划id 获取具体项
                List<DevMainBaseEntity> baseList = devMainBaseDao.getBaseInfo(entity.getId());
                for (DevMainBaseEntity base : baseList) {
                    DevPlanGcRecordEntity gcRecordEntity = new DevPlanGcRecordEntity();
                    gcRecordEntity.setBaseName(base.getName()); //选项名称
                    gcRecordEntity.setRelDevPlan(gc.getId());
                    gcRecordEntity.setBaseId(base.getId());//选项id
                    Integer rate = base.getFreRate();
                    gcRecordEntity.setFreRate(rate);//频率
                    //根据频率添加日期期限
                    Date start = gc.getStartTime();
                    Calendar curr = Calendar.getInstance();
                    curr.set(Calendar.HOUR_OF_DAY, 0);//控制时
                    curr.set(Calendar.MINUTE, 0);//控制分
                    curr.set(Calendar.SECOND, 0);//控制秒
                    curr.setTime(start);
                    gcRecordEntity.setStartTime(curr.getTime());
                    Date date = new Date();
                    if (rate == 1) {//每周,无法延期
                        Calendar curr1 = Calendar.getInstance();
                        curr1.set(Calendar.HOUR_OF_DAY, 0);//控制时
                        curr1.set(Calendar.MINUTE, 0);//控制分
                        curr1.set(Calendar.SECOND, 0);//控制秒
                        curr1.set(Calendar.DAY_OF_MONTH, curr1.get(Calendar.DAY_OF_MONTH) + 7);
                        gcRecordEntity.setDelayTime(curr1.getTime());
                    } else if (rate == 2) {//每两周
                        Calendar curr2 = Calendar.getInstance();
                        curr2.set(Calendar.HOUR_OF_DAY, 0);//控制时
                        curr2.set(Calendar.MINUTE, 0);//控制分
                        curr2.set(Calendar.SECOND, 0);//控制秒
                        curr2.set(Calendar.DAY_OF_MONTH, curr2.get(Calendar.DAY_OF_MONTH) + 14);
                        gcRecordEntity.setDelayTime(curr.getTime());
                    } else if (rate == 3) {//每月
                        Calendar curr3 = Calendar.getInstance();
                        curr3.set(Calendar.HOUR_OF_DAY, 0);//控制时
                        curr3.set(Calendar.MINUTE, 0);//控制分
                        curr3.set(Calendar.SECOND, 0);//控制秒
                        curr3.set(Calendar.MONTH, curr3.get(Calendar.MONTH) + 1);
                        gcRecordEntity.setDelayTime(curr3.getTime());
                    } else if (rate == 4) {//每个季度
                        Calendar curr4 = Calendar.getInstance();
                        curr4.set(Calendar.HOUR_OF_DAY, 0);//控制时
                        curr4.set(Calendar.MINUTE, 0);//控制分
                        curr4.set(Calendar.SECOND, 0);//控制秒
                        curr4.set(Calendar.MONTH, curr4.get(Calendar.MONTH) + 3);
                        gcRecordEntity.setDelayTime(curr4.getTime());
                    } else if (rate == 5) {//每半年
                        Calendar curr5 = Calendar.getInstance();
                        curr5.set(Calendar.HOUR_OF_DAY, 0);//控制时
                        curr5.set(Calendar.MINUTE, 0);//控制分
                        curr5.set(Calendar.SECOND, 0);//控制秒
                        curr5.set(Calendar.MONTH, curr5.get(Calendar.MONTH) + 6);

                        gcRecordEntity.setDelayTime(curr5.getTime());
                    } else if (rate == 6) {//一年
                        Calendar curr6 = Calendar.getInstance();
                        curr6.set(Calendar.HOUR_OF_DAY, 0);//控制时
                        curr6.set(Calendar.MINUTE, 0);//控制分
                        curr6.set(Calendar.SECOND, 0);//控制秒
                        curr.set(Calendar.YEAR, curr.get(Calendar.YEAR) + 1);
                        gcRecordEntity.setDelayTime(curr6.getTime());
                    }
                    gcRecordEntity.setEndTime(gc.getEndTime());
                    gcRecordEntity.setDelayTime(date);//最大延期时间
                    devPlanGcRecordService.save(gcRecordEntity);
                }
                if (gc != null) {
                    devPlanGcService.showT(gc.getId());
                }
            }
            //消息提醒
        }
        return devPlanMainDao.apply(devPlanMainEntity);
    }
}
